﻿using System;
using System.Linq;
using Sendz.Domain;
using System.Collections.Generic;
using System.Diagnostics;

namespace Sendz.Model
{
    public class EntityAutoResponderRepository : IAutoResponderRepository
    {
        #region AutoResponders /////////////////////////////

        public IList<AutoResponder> GetAutoRespondersByUserKey(Guid userKey)
        {
            using (SendzContext db = new SendzContext())
            {
                return db.AutoResponders.Where(ar => ar.UserKey == userKey && !ar.IsRemoved).ToList();
            }
        }

        public AutoResponder GetAutoResponderByKey(Guid autoResponderKey)
        {
            using (SendzContext db = new SendzContext())
            {
                var thisAutoResponder = db.AutoResponders.FirstOrDefault(ar => ar.AutoResponderKey == autoResponderKey);
                // Associate the related attachments for use in display etc. (use the common method to do this)
                var attachments = GetAttachments(autoResponderKey);
                if (attachments.Count() > 0)
                    thisAutoResponder.Attachments = attachments.ToList();
                return thisAutoResponder;
            }
        }

        // Same method for creation & save
        public bool SaveAutoResponder(AutoResponder autoResponder)
        {
            using (SendzContext db = new SendzContext())
            {
                try
                {
                    var thisAutoResponder = db.AutoResponders.FirstOrDefault(ar => ar.AutoResponderKey == autoResponder.AutoResponderKey);
                    if (thisAutoResponder != null)
                    {
                        thisAutoResponder.Name = autoResponder.Name;
                        thisAutoResponder.Subject = autoResponder.Subject;
                        thisAutoResponder.FromName = autoResponder.FromName;
                        thisAutoResponder.FromEmail = autoResponder.FromEmail;
                        thisAutoResponder.Body = autoResponder.Body;
                        // Update the last modified date
                        thisAutoResponder.LastModified = DateTime.Now;
                    }
                    else
                    {
                        // Update the last modified date so new autoResponders can also appear at top of sort order
                        autoResponder.LastModified = DateTime.Now;
                        db.AutoResponders.AddObject(autoResponder);
                    }
                    db.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public bool RemoveAutoResponder(Guid autoResponderKey)
        {
            using (SendzContext db = new SendzContext())
            {
                try
                {
                    var thisAutoResponder = db.AutoResponders.Single(ar => ar.AutoResponderKey == autoResponderKey);
                    // Logical delete, not physical delete
                    thisAutoResponder.IsRemoved = true;
                    // Update the last modified date
                    thisAutoResponder.LastModified = DateTime.Now;
                    db.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        #endregion

        #region Attachments ////////////////////////

        public bool AddAttachment(Attachment attachment)
        {
            using (SendzContext db = new SendzContext())
            {
                try
                {
                    // Indicate that the autoresponder was modified
                    var thisAutoResponder = db.AutoResponders.Single(ar => ar.AutoResponderKey == attachment.AutoResponderKey);
                    thisAutoResponder.LastModified = DateTime.Now;
                    db.Attachments.AddObject(attachment);
                    db.SaveChanges();
                }
                catch
                {
                    return false;
                }
                return true;
            }
        }

        public IList<Attachment> GetAttachments(Guid autoResponderKey)
        {
            using (SendzContext db = new SendzContext())
            {
                return db.Attachments.Where(at => at.AutoResponderKey == autoResponderKey && !at.IsRemoved).ToList();
            }
        }

        // Pass back attachment object so the file deletion can occur
        public Attachment RemoveAttachment(Guid attachmentKey)
        {
            using (SendzContext db = new SendzContext())
            {
                try
                {
                    var thisAttachment = db.Attachments.Single(at => at.AttachmentKey == attachmentKey);
                    thisAttachment.LastModified = DateTime.Now;
                    thisAttachment.IsRemoved = true;
                    // Indicate that the autoresponder was modified
                    var thisAutoResponder = db.AutoResponders.Single(ar => ar.AutoResponderKey == thisAttachment.AutoResponderKey);
                    thisAutoResponder.LastModified = DateTime.Now;
                    db.SaveChanges();
                    return thisAttachment;
                }
                catch
                {
                    return null;
                }
            }
        }

        #endregion
    }
}
